<?php
	function get_excel_data($filePath)
	{
		require_once '/PHPExcel/Classes/PHPExcel.php';
		// $filePath = 'data/lg1.xls';
	    $PHPReader = new \PHPExcel_Reader_Excel2007();
	    if(!$PHPReader->canRead($filePath)){
	        $PHPReader = new \PHPExcel_Reader_Excel5();
	        if(!$PHPReader->canRead($filePath)){
	            exit("excel读取失败！");
	        }
	    }
	    $PHPExcel = $PHPReader->load($filePath);
	    $currentSheet = $PHPExcel->getSheet(0);
	    $sheetInfo = $currentSheet->toArray(null, true, false, false,false);
	    $emptyRowCount = $currentSheet->getHighestRow() - count($sheetInfo);  // 看下去掉了多少条空行
	    unset($sheetInfo[0]);
	    return $sheetInfo;
	}
	// 把数据放到txt，好处理
	function array_data_write_txt()
	{
		$data = get_excel_data('data/lg1.xls');
		foreach ($data as $key => $value) {
			// var_dump($value);
			$value[1] = substr($value[1], 0,10);
	    	$str = $value[0].' '.$value[1];
	    	$str = str_replace(PHP_EOL, '', $str);
	    	file_put_contents('deal_data/data.txt', $str.PHP_EOL,FILE_APPEND);
	    }
	    // 去除末尾的换行符
		$str = file_get_contents('deal_data/data.txt');
		$str = chop($str,PHP_EOL);
		file_put_contents('deal_data/data.txt', $str);
	}
	function read_txt($file_path)
	{
		
		$fp = fopen($file_path,'r');
		$i=0;
		$app_name_array = array();
		while(!feof($fp))
		{
			 $app_name_array[$i] = fgets($fp);
			 $i++;
		}
		return $app_name_array;
	}
	// 读取本地文本数据，并转为数组返回，二维数组有名字和时间
	function read_txt2($file_path)
	{
		
		$fp = fopen($file_path,'r');
		$i=0;
		$app_name_array = array();
		while(!feof($fp))
		{
			$str = fgets($fp);
			$arr = explode(' ', $str);
			$app_name_array[$i]['name'] = $arr[0];
			$app_name_array[$i]['time'] = $arr[1];
			$i++;
		}
		return $app_name_array;
	}

	//用于读取excel数据，并把其中的app名字放到一个txt文件里面 
	function read_app_name()
	{
		require_once '/PHPExcel/Classes/PHPExcel.php';
		$filePath = 'data/lg1.xls';
	    $PHPReader = new \PHPExcel_Reader_Excel2007();
	    if(!$PHPReader->canRead($filePath)){
	        $PHPReader = new \PHPExcel_Reader_Excel5();
	        if(!$PHPReader->canRead($filePath)){
	            exit("excel读取失败！");
	        }
	    }
	    $PHPExcel = $PHPReader->load($filePath);
	    $currentSheet = $PHPExcel->getSheet(0);
	    $sheetInfo = $currentSheet->toArray(null, true, false, false,false);
	    $emptyRowCount = $currentSheet->getHighestRow() - count($sheetInfo);  // 看下去掉了多少条空行
	    unset($sheetInfo[0]);
	    // var_dump($sheetInfo);
	    $app_name_array = array();
	    $i=0;
	    $put_file_app_name = 'deal_data/app_name_array.txt';
	    $put_file_all_app = 'deal_data/data.txt';
	    foreach ($sheetInfo as $key => $value) {
	    	file_put_contents($put_file_all_app,$value[0].PHP_EOL,FILE_APPEND );
	    	if(!in_array($value[0], $app_name_array)){
	    		$app_name_array[$i] = $value[0];
	    		file_put_contents($put_file_app_name,$value[0].PHP_EOL,FILE_APPEND );
	    		$i++;
	    	}
	    }
	    // 去除换行符
	    $str = file_get_contents('deal_data/data_num.txt');
		$str = chop($str,PHP_EOL);
	}
	
	// app名字量化为数字
	function app_to_num()
	{
		$app_name_array = array();
		$app_name_array = read_txt('deal_data/app_name_array.txt');
		$data = array();
		$data = read_txt('deal_data/data.txt');
		$i=0;
		$map = array();
		$i=0;
		foreach ($app_name_array as $key => $app) 
		{
			$map[$app]=$i;
			file_put_contents('deal_data/app_name_array_num.txt',$i.PHP_EOL,FILE_APPEND);
			$i++;
		}
		$str = file_get_contents('deal_data/app_name_array_num.txt');
		$str = chop($str,PHP_EOL);
		file_put_contents('deal_data/app_name_array_num.txt', $str);
		
		foreach ($data as $key => $app1) 
		{
			// 去除换行符
			$str = $map[$app1];
			$str = str_replace(PHP_EOL, '', $str);
			file_put_contents('deal_data/data_num.txt',$str.PHP_EOL,FILE_APPEND);
		}
		// 去除末尾的换行符
		$str = file_get_contents('deal_data/data_num.txt');
		$str = chop($str,PHP_EOL);
		file_put_contents('deal_data/data_num.txt', $str);
	}

	// // 初始化统计app数组
	// function init_count_next_app()
	// {
	// 	$app_name_array = read_txt('deal_data/app_name_array.txt');
	// 	$app_next_count = array();
	// 	$i=0;
	// 	foreach ($app_name_array as $key0 => $value0) 
	// 	{
	// 		$app_next_count[$value0]['sum'] = 0;
	// 		foreach ($app_name_array as $key1 => $value1) {
	// 			$app_next_count[$value0][$value1] = 0;
	// 			$i++;
	// 		}
	// 	}
	// 	return $app_next_count;
	// }
	// 统计使用这个app，然后使用下一个app的概率
	function used_app_predict_next_app($j,$data)
	{
		$all_app = $data;
		$app_next_count = array();
		
		$i=0;
		$count = count($all_app);
		// 最后一个数据不能去读取，因为他没有下一个app了
		$count-=2;
		// var_dump($all_app);
		foreach ($all_app as $key => $app) 
		{
			if($key >= $count){
				break;
			}
			if(empty($app_next_count[$all_app[$key]]['sum'])){
				$app_next_count[$all_app[$key]]['sum'] = 1;
			}else{
				$app_next_count[$all_app[$key]]['sum'] += 1;
			}
			if(empty($app_next_count[$all_app[$key]][$all_app[$key+$j]])){
				$app_next_count[$all_app[$key]][$all_app[$key+$j]] = 1;
			}else{
				$app_next_count[$all_app[$key]][$all_app[$key+$j]] += 1;
			}
		}
		
		foreach ($app_next_count as $app1 => $value0) 
		{
			$sum = $value0['sum'];
			if($sum==0){
				continue;
			}
			foreach ($value0 as $app2 => $value1) 
			{
				if($value1==0){
					continue;
				}
				$app_next_count[$app1][$app2] = $value1/$sum;
			}
		}
		return $app_next_count;
	}
	// echo count(used_app_predict_next_app(2),COUNT_RECURSIVE);
	// 初始化3个app
	// function init_app3()
	// {
	// 	$app_name_array = read_txt('deal_data/app_name_array.txt');
	// 	$app_count_3 = array();
	// 	foreach ($app_name_array as $key0 => $value0) 
	// 	{
	// 		foreach ($app_name_array as $key1 => $value1) 
	// 		{
	// 			$app_count_3[$value0][$value1]['sum']=0;
	// 			foreach ($app_name_array as $key2 => $value2) 
	// 			{
	// 				$app_count_3[$value0][$value1][$value2]=0;
	// 			}
	// 		}
	// 	}
	// 	return $app_count_3;
	// }
	
	function app1_app2_hasused_predict_app3($data)
	{
		$app_count_3 = array();
		$all_app = $data;
		$i=0;
		$count = count($all_app);
		// var_dump($app_count_3);
		// 最后一个数据不能去读取，因为他没有下一个app了
		$count-=2;
		foreach ($all_app as $key => $app) 
		{
			if($key >= $count){
				break;
			}
			if(empty($app_count_3[$all_app[$key]][$all_app[$key+1]]['sum'])){
				$app_count_3[$all_app[$key]][$all_app[$key+1]]['sum'] = 1;
			}else{
				$app_count_3[$all_app[$key]][$all_app[$key+1]]['sum'] += 1;
			}
			if(empty($app_count_3[$all_app[$key]][$all_app[$key+1]][$all_app[$key+2]])){
				$app_count_3[$all_app[$key]][$all_app[$key+1]][$all_app[$key+2]] = 1;
			}else{
				$app_count_3[$all_app[$key]][$all_app[$key+1]][$all_app[$key+2]] += 1;
			}
		}

		// var_dump($app_count_3);
		foreach ($app_count_3 as $app1 => $value1) 
		{
			foreach ($value1 as $app2 => $value2) 
			{
				$sum = $app_count_3[$app1][$app2]['sum'];
				
				foreach ($value2 as $app3 => $value3) {
					$app_count_3[$app1][$app2][$app3]/=$sum;
				}

			}
		}
		
		return $app_count_3;
	}
	// 最终提取的数据
	function union($data){
		set_time_limit(30);
		$app_count_3 = app1_app2_hasused_predict_app3($data);
		$app1_app3 = used_app_predict_next_app(1,$data);
		$app2_app3 = used_app_predict_next_app(2,$data);
		var_dump($app_count_3);
		// var_dump($app1_app3);
		// var_dump($app2_app3);

		$train_data_file = 'deal_data/train_data.txt';
		$odds = array();
		foreach ($app_count_3 as $app1 => $value1) 
		{
			foreach ($value1 as $app2 => $value2) 
			{
				foreach ($value2 as $app3 => $value3) 
				{
					if($app3=='sum'||empty($app1_app3[$app1][$app3])||empty($app2_app3[$app2][$app3]))
						continue;
					$odds[$app1][$app2][$app3]['0'] = $app1_app3[$app1][$app3];
					
					$odds[$app1][$app2][$app3]['1'] = $app2_app3[$app2][$app3];
					
					$odds[$app1][$app2][$app3]['2'] = $app_count_3[$app1][$app2][$app3];
					
					$str = $odds[$app1][$app2][$app3]['0'].' '.$odds[$app1][$app2][$app3]['1'].' '.$odds[$app1][$app2][$app3]['2'];
					echo $str."<br>";
					$str = str_replace(PHP_EOL, '', $str);
					file_put_contents($train_data_file,$str.PHP_EOL,FILE_APPEND);
				}
			}
		}
		// 去除末尾的换行符
		$str = file_get_contents('deal_data/data_num.txt');
		$str = chop($str,PHP_EOL);
		file_put_contents('deal_data/data_num.txt', $str);
		
		
	}
	function deal_data(){
		$data = read_txt2('deal_data/data.txt');
		$app_data = array();
		$i=0;
		foreach ($data as $key => $value)
		{
			$app_data[$i] = $value['name'];
			if(empty($data[$key+1]['time'])){
				break;
			}
			if($data[$key]['time']!=$data[$key+1]['time']){
				$i=0;
				// var_dump($app_data);
				// 处理数据
				union($app_data);
				unset($app_data);
				$app_data = array();
			}else{
				$i++;
			}
			
		}
	}
	deal_data();
?>